﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class giaovien_DanhGiaHanhKiem : System.Web.UI.Page
{
    SoLienLacDataContext db = new SoLienLacDataContext();
    clsLoadCombobox cls = new clsLoadCombobox();
    clsKiemTraTrangThai clskttt = new clsKiemTraTrangThai();
    string manam;
    string malop;
    protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["Username"] == null || Session["Username"].ToString() == "")
        {
            Response.Redirect("~/GiaoDienClient/TrangChu.aspx");
        }
        else
        {
            manam = Request.QueryString["MaNam"];
            malop = Request.QueryString["MaLop"];
            if (!IsPostBack)
            {
                string magv = Session["Username"].ToString();
                cls.LoadComboboxNamHoc(drNamHoc);
                cls.LoadComboboxLopHocTheoMaGV(drLopHoc, int.Parse(drNamHoc.SelectedValue.ToString()),magv);
                drNamHoc.SelectedValue = manam;
                drLopHoc.SelectedValue = malop;
                int manam1 = int.Parse(drNamHoc.SelectedValue.ToString());
                int malop1 = int.Parse(drLopHoc.SelectedValue.ToString());
                int maky = 1;
                if (rdoHocKy2.Checked == true) { maky = 2; }
                lblTrangThai.Text = clskttt.trangthaibanghanhkiem(manam1, malop1, maky);
                if (lblTrangThai.Text == "Đã duyệt")
                {
                    clskttt.NgayDuyetBDHK(manam1, malop1, maky, lblNgayDuyet);
                }
                LayDuLieuTuBangKQTK(int.Parse(drNamHoc.SelectedValue.ToString()), maky, int.Parse(drLopHoc.SelectedValue.ToString()));
                btnGuiHK.Visible = false;
            }
        }
    }
    protected void btnTimKiem_Click(object sender, EventArgs e)
    {
        int manam = int.Parse(drNamHoc.SelectedValue.ToString());
        int malop = int.Parse(drLopHoc.SelectedValue.ToString());
        int maky = 0;
        if (rdoHocKy1.Checked == true)
        {
            maky = 1;
            lblTrangThai.Text = clskttt.trangthaibanghanhkiem(manam, malop, maky);
            if (lblTrangThai.Text == "Đã duyệt")
            {
                clskttt.NgayDuyetBDHK(manam, malop, maky, lblNgayDuyet);
            }
            LayDuLieuTuBangKQTK(manam, maky, malop);
            
        }
        if (rdoHocKy2.Checked == true)
        {
            maky = 2;
            lblTrangThai.Text = clskttt.trangthaibanghanhkiem(manam, malop, maky);
            if (lblTrangThai.Text == "Đã duyệt")
            {
                clskttt.NgayDuyetBDHK(manam, malop, maky, lblNgayDuyet);
            }
            LayDuLieuTuBangKQTK(manam,maky,malop);
        }
        btnGuiHK.Visible = false;

    }
    public void LayDuLieuTuBangKQTK(int manam,int maky, int malop)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("MaHS",typeof(string));
        dt.Columns.Add("TenHS", typeof(string));
        dt.Columns.Add("DiemTongKet",typeof(string));
        dt.Columns.Add("HanhKiemTot",typeof(bool));
        dt.Columns.Add("HanhKiemKha",typeof(bool));
        dt.Columns.Add("HanhKiemTB",typeof(bool));
        dt.Columns.Add("HanhKiemYeu",typeof(bool));
        var c=from p in db.funcTraVeDanhSachHSBangKQHocTap(manam,maky,malop) select p;
        if (c.Count() != 0)
        {
            foreach (var con in c)
            {
                DataRow dr = dt.NewRow();
                dr[0] = con.MaHS.ToString();
                dr[1] = con.TenHS.ToString();
                if (con.DiemTongKet != null)
                { 
                    dr[2] = con.DiemTongKet.ToString(); 
                }
                else { dr[2] = ""; }
                if (con.HanhKiem !=null)
                {
                    if (con.HanhKiem == "T")
                    { dr[3] = true; dr[4] = false; dr[5] = false; dr[6] = false; }
                    if (con.HanhKiem == "K")
                    { dr[4] = true; dr[3] = false; dr[5] = false; dr[6] = false; }
                    if (con.HanhKiem == "TB")
                    { dr[5] = true; dr[4] = false; dr[3] = false; dr[6] = false; }
                    if (con.HanhKiem == "Y")
                    { dr[6] = true; dr[4] = false; dr[5] = false; dr[3] = false; }
                }
                if (con.HanhKiem == null)
                {
                    dr[3] = false;
                    dr[4] = false;
                    dr[5] = false;
                    dr[6] = false;
                }
                dt.Rows.Add(dr);

                
            }
            grvHanhKiem.DataSource = dt;
            grvHanhKiem.DataBind();
        }
    }
    protected void btnLuuHK_Click(object sender, EventArgs e)
    {
        btnGuiHK.Visible = true;
        int manam = int.Parse(drNamHoc.SelectedValue.ToString());
        int malop = int.Parse(drLopHoc.SelectedValue.ToString());
        int maky = 0;
        if (rdoHocKy1.Checked == true)
        { maky = 1; }
        if (rdoHocKy2.Checked == true)
        { maky = 2; }
        LayDuLieuTuGridView(manam, maky);
        lblTrangThai.Text = clskttt.trangthaibanghanhkiem(manam, malop, maky);
        if (lblTrangThai.Text == "Đã duyệt")
        {
            clskttt.NgayDuyetBDHK(manam, malop, maky, lblNgayDuyet);
        }
        //string tengv=Session["Username"].ToString();
        //if(tengv!="" ||tengv!=null)
        //{
        //    //var c=from p in db.CanBoGiaoViens where p.MaCanBoGV==tengv select p.TenCanBo;
        //    //TacVus tv = new TacVus();
        //    //tv.TenTacVu = "Cập nhật hạnh kiểm";
        //    //tv.NoiDung = "Giáo viên(Mã GV" +tengv+ ") " + c.First().ToString() + " " + "cập nhật hạnh kiểm lớp" +" "+drLopHoc.SelectedItem.Text;
        //    //tv.NgayThang = DateTime.Now;
        //    //db.TacVus.InsertOnSubmit(tv);
        //    //db.SubmitChanges();
        //}
        //string tentv = "Cập nhật hạnh kiểm";
        //string macbgv = Session["Username"].ToString();
        //string noidung = "Đã cập nhật hạnh kiểm của lớp" + drLopHoc.SelectedItem.Text;
        //clsCapNhatTacVu tacvu = new clsCapNhatTacVu();
        //tacvu.ThemTacVu(tentv, noidung, DateTime.Now, macbgv);

    }
    private void LayDuLieuTuGridView(int manam, int maky)
    {

        for (int i = 0; i < grvHanhKiem.Rows.Count; i++)
        {
            Label lblMaHS = (Label)grvHanhKiem.Rows[i].FindControl("lblMaHS");
            Label lblTenHS = (Label)grvHanhKiem.Rows[i].FindControl("lblTenHS");
            Label lblDTK = (Label)grvHanhKiem.Rows[i].FindControl("lblDTK");
            RadioButton rdoHKT = (RadioButton)grvHanhKiem.Rows[i].FindControl("rdoHKT");
            RadioButton rdoHKK = (RadioButton)grvHanhKiem.Rows[i].FindControl("rdoHKK");
            RadioButton rdoHKTB = (RadioButton)grvHanhKiem.Rows[i].FindControl("rdoHKTB");
            RadioButton rdoHKY = (RadioButton)grvHanhKiem.Rows[i].FindControl("rdoHKY");
            if (rdoHKT.Checked == true)
            {
                db.procCapNhatHanhKiemHS(manam, maky, lblMaHS.Text, "T");
            }
            if (rdoHKK.Checked == true)
            { db.procCapNhatHanhKiemHS(manam,maky,lblMaHS.Text,"K");}
            if (rdoHKTB.Checked == true)
            { db.procCapNhatHanhKiemHS(manam, maky, lblMaHS.Text, "TB");
            }
            if (rdoHKY.Checked == true)
            { db.procCapNhatHanhKiemHS(manam,maky,lblMaHS.Text,"Y"); }
            if (rdoHKT.Checked == false && rdoHKK.Checked == false && rdoHKTB.Checked == false && rdoHKY.Checked == false)
            { db.procCapNhatHanhKiemHS(manam, maky, lblMaHS.Text, null); }
        }

    }
    private void CheckToanBo(CheckBox ckb)
    {
        for (int i = 0; i < grvHanhKiem.Rows.Count; i++)
        {
            CheckBox ck = (CheckBox)grvHanhKiem.Rows[i].FindControl("ckCheck");
            if (ckb.Checked == true)
            {
                ck.Checked = true;
            }
            if (ckb.Checked == false)
            { ck.Checked = false; }
            
        }
        
    }
    private void DanhGiaHKToanBo(string hanhkiem,int manam,int maky)
    {
        for (int i = 0; i < grvHanhKiem.Rows.Count; i++)
        {
            CheckBox ck = (CheckBox)grvHanhKiem.Rows[i].FindControl("ckCheck");
            Label lblMahs = (Label)grvHanhKiem.Rows[i].FindControl("lblMaHS");
            if (ck.Checked == true)
            {
                db.procCapNhatHanhKiemHS(manam, maky, lblMahs.Text, hanhkiem);
            }

        }
    }
    protected void ckCheckAll2_CheckedChanged(object sender, EventArgs e)
    {
        CheckToanBo(ckCheckAll2);
    }
    protected void ckToanBo_CheckedChanged(object sender, EventArgs e)
    {
        CheckToanBo(ckToanBo);
    }
    private void LuuHanhKiemToanBo(string hanhkiem)
    {
        int manam = int.Parse(drNamHoc.SelectedValue.ToString());
        int ky = 1;
        if (rdoHocKy1.Checked == true)
        { ky = 1; }
        if (rdoHocKy2.Checked == true)
        { ky = 2; }
        DanhGiaHKToanBo(hanhkiem, manam, ky);
        LayDuLieuTuBangKQTK(manam,ky,int.Parse(drLopHoc.SelectedValue.ToString()));
    }
    protected void rdoTot1_CheckedChanged(object sender, EventArgs e)
    {
        LuuHanhKiemToanBo("T");
    }
    protected void rdoKha1_CheckedChanged(object sender, EventArgs e)
    {
        LuuHanhKiemToanBo("K");
    }
    protected void rdoTB1_CheckedChanged(object sender, EventArgs e)
    {
        LuuHanhKiemToanBo("TB");
    }
    protected void rdoYeu1_CheckedChanged(object sender, EventArgs e)
    {
        LuuHanhKiemToanBo("Y");
    }
    protected void rdoTot2_CheckedChanged(object sender, EventArgs e)
    {
        LuuHanhKiemToanBo("T");
    }
    protected void rdoKha2_CheckedChanged(object sender, EventArgs e)
    {
        LuuHanhKiemToanBo("K");
    }
    protected void rdoTB2_CheckedChanged(object sender, EventArgs e)
    {
        LuuHanhKiemToanBo("TB");
    }
    protected void rdoYeu2_CheckedChanged(object sender, EventArgs e)
    {
        LuuHanhKiemToanBo("Y");
    }
    protected void drLopHoc_SelectedIndexChanged(object sender, EventArgs e)
    {
        int manam1 = int.Parse(drNamHoc.SelectedValue.ToString());
        int malop1 = int.Parse(drLopHoc.SelectedValue.ToString());
        int maky1 = 1;
        if (rdoHocKy2.Checked == true)
        { maky1 = 2; }
        LayDuLieuTuBangKQTK(manam1,maky1,malop1);
        lblTrangThai.Text = clskttt.trangthaibanghanhkiem(manam1, malop1, maky1);
        if (lblTrangThai.Text == "Đã duyệt")
        {
            clskttt.NgayDuyetBDHK(manam1, malop1, maky1, lblNgayDuyet);
        }
    }
    protected void rdoHocKy1_CheckedChanged(object sender, EventArgs e)
    {
        int manam1 = int.Parse(drNamHoc.SelectedValue.ToString());
        int malop1 = int.Parse(drLopHoc.SelectedValue.ToString());
        LayDuLieuTuBangKQTK(int.Parse(drNamHoc.SelectedValue.ToString()), 1, int.Parse(drLopHoc.SelectedValue.ToString()));
        lblTrangThai.Text = clskttt.trangthaibanghanhkiem(manam1, malop1, 1);
        if (lblTrangThai.Text == "Đã duyệt")
        {
            clskttt.NgayDuyetBDHK(manam1, malop1, 1, lblNgayDuyet);
        }
    }
    protected void rdoHocKy2_CheckedChanged(object sender, EventArgs e)
    {
        int manam1 = int.Parse(drNamHoc.SelectedValue.ToString());
        int malop1 = int.Parse(drLopHoc.SelectedValue.ToString());
        LayDuLieuTuBangKQTK(int.Parse(drNamHoc.SelectedValue.ToString()), 2, int.Parse(drLopHoc.SelectedValue.ToString()));
        lblTrangThai.Text = clskttt.trangthaibanghanhkiem(manam1, malop1, 2);
        if (lblTrangThai.Text == "Đã duyệt")
        {
            clskttt.NgayDuyetBDHK(manam1, malop1, 2, lblNgayDuyet);
        }
    }
    protected void grvHanhKiem_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int manam=int.Parse(drNamHoc.SelectedValue.ToString());
        int malop=int.Parse(drLopHoc.SelectedValue.ToString());
        int maky=1;
        if(rdoHocKy2.Checked==true)
        {maky=2;}
        int hangchon = e.RowIndex;
        RadioButton rdoHKT = (RadioButton)grvHanhKiem.Rows[hangchon].FindControl("rdoHKT");
        RadioButton rdoHKK = (RadioButton)grvHanhKiem.Rows[hangchon].FindControl("rdoHKK");
        RadioButton rdoHKTB = (RadioButton)grvHanhKiem.Rows[hangchon].FindControl("rdoHKTB");
        RadioButton rdoHKY = (RadioButton)grvHanhKiem.Rows[hangchon].FindControl("rdoHKY");
        Label lblTenHS = (Label)grvHanhKiem.Rows[hangchon].FindControl("lblTenHS");
        rdoHKK.Checked = false;
        rdoHKT.Checked = false;
        rdoHKTB.Checked = false;
        rdoHKY.Checked = false;
        LayDuLieuTuGridView(manam, maky);
        LayDuLieuTuBangKQTK(manam, maky, malop);
        string tengv = Session["Username"].ToString();
        if (tengv != "" || tengv != null)
        {
           
            string tentv = "Cập nhật kiểm";
            string macbgv = Session["Username"].ToString();
            string noidung = "Đã cập nhật hạnh kiểm của lớp" + drLopHoc.SelectedItem.Text;
            clsCapNhatTacVu tacvu = new clsCapNhatTacVu();
            tacvu.ThemTacVu(tentv, noidung, DateTime.Now, tengv);
        }

    }
    protected void grvHanhKiem_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        
    }
    protected void grvHanhKiem_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int manam = int.Parse(drNamHoc.SelectedValue.ToString());
        int malop = int.Parse(drLopHoc.SelectedValue.ToString());
        int maky = 1;
        if (rdoHocKy2.Checked == true)
        { maky = 2; }
        int hangchon = e.NewEditIndex;
        Label lblMaHS = grvHanhKiem.Rows[hangchon].FindControl("lblMaHS") as Label;
        RadioButton rdoHKT = (RadioButton)grvHanhKiem.Rows[hangchon].FindControl("rdoHKT");
        RadioButton rdoHKK = (RadioButton)grvHanhKiem.Rows[hangchon].FindControl("rdoHKK");
        RadioButton rdoHKTB = (RadioButton)grvHanhKiem.Rows[hangchon].FindControl("rdoHKTB");
        RadioButton rdoHKY = (RadioButton)grvHanhKiem.Rows[hangchon].FindControl("rdoHKY");
        //LayDuLieuTuGridView(manam, maky);
        if (rdoHKT.Checked == true)
        {
            db.procCapNhatHanhKiemHS(manam, maky, lblMaHS.Text, "T");
        }
        if (rdoHKK.Checked == true)
        { db.procCapNhatHanhKiemHS(manam, maky, lblMaHS.Text, "K"); }
        if (rdoHKTB.Checked == true)
        {
            db.procCapNhatHanhKiemHS(manam, maky, lblMaHS.Text, "TB");
        }
        if (rdoHKY.Checked == true)
        { db.procCapNhatHanhKiemHS(manam, maky, lblMaHS.Text, "Y"); }
        if (rdoHKT.Checked == false && rdoHKK.Checked == false && rdoHKTB.Checked == false && rdoHKY.Checked == false)
        { db.procCapNhatHanhKiemHS(manam, maky, lblMaHS.Text, null); }
        LayDuLieuTuBangKQTK(manam, maky, malop);
        string tengv = Session["Username"].ToString();
        if (tengv != "" || tengv != null)
        {

            string tentv = "Cập nhật kiểm";
            string macbgv = Session["Username"].ToString();
            string noidung = "Đã cập nhật hạnh kiểm của lớp" + drLopHoc.SelectedItem.Text;
            clsCapNhatTacVu tacvu = new clsCapNhatTacVu();
            tacvu.ThemTacVu(tentv, noidung, DateTime.Now, tengv);
        }
    }
    protected void btnGuiHK_Click(object sender, EventArgs e)
    {
        int manam = int.Parse(drNamHoc.SelectedValue.ToString());
        int malop = int.Parse(drLopHoc.SelectedValue.ToString());
        int maky = 1;
        if (rdoHocKy1.Checked == true)
        {
            maky = 1;
        }
        if (rdoHocKy2.Checked == true)
        {
            maky = 2;
        }
        if (clskttt.kiemtrabdhk(manam, malop, maky) == false)
        {
            TrangThaiBangDiemTheoKy bdhk = new TrangThaiBangDiemTheoKy();
            bdhk.MaNam = manam;
            bdhk.MaLop = malop;
            bdhk.MaKy = maky;
            
            bdhk.NgayGuiHK = DateTime.Now.Date;
            db.TrangThaiBangDiemTheoKies.InsertOnSubmit(bdhk);
            db.SubmitChanges();
            lblTrangThai.Text = clskttt.trangthaibanghanhkiem(manam, malop, maky);
            clskttt.NgayGuiBangHanhKiem(manam, malop, maky, lblNgayGui);
            lblNgayDuyet.Text = "";
        }
        if (clskttt.kiemtrabdhk(manam, malop, maky) == true)
        {
            TrangThaiBangDiemTheoKy bdhk = db.TrangThaiBangDiemTheoKies.SingleOrDefault(p => p.MaKy == maky && p.MaLop == malop && p.MaNam == manam);
            
            bdhk.NgayGui = DateTime.Now.Date;
            
            db.SubmitChanges();
            lblTrangThai.Text = clskttt.trangthaibanghanhkiem(manam, malop, maky);
            clskttt.NgayGuiBangHanhKiem(manam, malop, maky, lblNgayGui);
            lblNgayDuyet.Text = "";
        }
    }
}
